package com.mall.shopping.services;

import com.mall.shopping.RecommendService;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


//查询推荐商品

@Component
@Service
public class RecommendImp implements RecommendService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;

    @Override
    public List<PanelDto> recommend() {
        List<PanelDto> panelDtos = new ArrayList<>();
        Example example = new Example(Panel.class);
        example.createCriteria().andEqualTo("position",1);

        List<Panel> panels = panelMapper.selectByExample(example);
        for (Panel panel : panels) {
            Integer panelId = panel.getId();
            List<PanelContentItemDto> panelContentItemDtos = panelContentMapper.selectPanelContentItemsByPanelId(panelId);

            PanelDto panelDto = new PanelDto();

            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDto.setId(panelId);
            panelDto.setLimitNum(panel.getLimitNum());
            panelDto.setName(panel.getName());
            panelDto.setPosition(panel.getPosition());
            panelDto.setRemark(panel.getRemark());
            panelDto.setSortOrder(panel.getSortOrder());
            panelDto.setStatus(panel.getStatus());
            panelDto.setType(panel.getType());

            panelDtos.add(panelDto);
        }

        return panelDtos;

    }
}
